import {
  getBranchTreeList,
  deleteStructure,
  queryUsersList,
  addUsers,
  existUserName,
  userDetailsShow,
  updataUsers,
  detailsStructure,
  getProductDiviList,
  createBranch,
  findByCustomerId,
  deleteMessageById,
  deleteMessageByIds,
  getRuleTable,
  getPersonnes,
  assignPersonne,
  selectAllot,
  systemCommitAssign,
  getRoluesTree,
  getNodeAssignRole,
  getParentBranchTreeList,
  nodeAssignUpdate,
  saveTree,
  updateBranch,
} from '../services/api';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
export default {
  namespace: 'system',

  state: {
    allBranchTreeList: null,
    userList: [],
    streuctureLoading: false,
    systemLoading: false,
    streuctureDetails: {},
    userLoading: false,
    userPage: {},
    userNameResult: {},
    userDetails: null,
    detailValue: {},
    structureId: '',
    structureBranchList: {},
    stateStructure: '',
    systemDetailValue: {},
    systemResult: {},
    messageNotificationList: [],
    messagePage: {},
    systemRuleList: [],
    systemAssignedList: [],
    systemAssignorList: [],
    systemRoluesTreeList: null,
    systemNodeAssignRoleList: null,
    systemBranchId: null,
    systemProductDiviList: null,
    systemEditProductDiviList: null,
    branchTreeList: null,
  },

  effects: {
    //上级组织
    *fetchBasic(_, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: true,
        },
      });
      const response = yield call(getParentBranchTreeList);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            streuctureLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          branchTreeList: response.data,
        },
      });
    },
    //全部分公司
    *getBranchTreeList({ payload, callback }, { call, put }) {
      const response = yield call(getBranchTreeList, payload);
      if (!isResponerCode(response)) {
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          allBranchTreeList: response.data,
        },
      });
      if (callback) callback();
    },

    //新增组织
    *createBranch({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: true,
        },
      });
      const response = yield call(createBranch, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            streuctureLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          systemResult: response.result,
        },
      });
      if (callback) callback();
    },

    //修改组织
    *updateBranch({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: true,
        },
      });
      const response = yield call(updateBranch, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            streuctureLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          systemResult: response.result,
        },
      });
      if (callback) callback();
    },

    //删除组织
    *deleteStructure({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: true,
        },
      });
      const response = yield call(deleteStructure, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            streuctureLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: false,
        },
      });
      if (response.result.code == '200') {
        message.success('删除成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'show',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback();
    },

    //显示用户列表
    *fetchUsers({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(queryUsersList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          userList: response.data.list,
          userPage: response.data.page,
        },
      });
      if (callback) callback();
    },
    //添加用户
    *addUsers({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(addUsers, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      if (response.result.code == 200) {
        message.success('添加成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'show',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback();
    },
    //修改用户详情
    *updataUsers({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(updataUsers, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      if (response.result.code == 200) {
        message.success('修改成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'show',
        payload: {
          data: response.data,
        },
      });
      if (callback) callback();
    },
    //编辑获取组织架构详情
    *detailsStructure({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: true,
        },
      });
      const response = yield call(detailsStructure, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            streuctureLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          streuctureLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          structureBranchList: response.data,
          systemEditProductDiviList: response.data.productList,
        },
      });
      if (callback) callback();
    },
    //检测用户名是否重复
    *existUserName({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(existUserName, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          userNameResult: response.result,
        },
      });
      if (callback) callback();
    },
    //获取用户详情
    *userDetailsShow({ payload, callback }, { call, put }) {
      const response = yield call(userDetailsShow, payload);
      if (!isResponerCode(response)) {
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userDetails: response.data,
        },
      });
      if (callback) callback();
    },

    //获取消息列表
    *findByCustomerId({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          systemLoading: true,
        },
      });
      const response = yield call(findByCustomerId, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            systemLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          systemLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          messageNotificationList: response.data.list,
          messagePage: response.data.page,
        },
      });
      if (callback) callback();
    },

    //删除消息
    *deleteMessageById({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          systemLoading: true,
        },
      });
      const response = yield call(deleteMessageById, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            systemLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          systemLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          userNameResult: response.result,
        },
      });
      if (callback) callback();
    },

    //批量删除消息
    *deleteMessageByIds({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          systemLoading: true,
        },
      });
      const response = yield call(deleteMessageByIds, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            systemLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          systemLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          userNameResult: response.result,
        },
      });
      if (callback) callback();
    },

    //获取分配规则制定表
    *getRuleTable({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          systemLoading: true,
        },
      });
      const response = yield call(getRuleTable, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            systemLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          systemLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          systemRuleList: response.data,
          // userNameResult: response.result
        },
      });
      if (callback) callback();
    },

    //指派人员列表
    *getPersonnes({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(getPersonnes, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          systemAssignedList: response.data,
        },
      });
      if (callback) callback();
    },

    //指派人员提交
    *assignPersonne({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(assignPersonne, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          systemResult: response.result,
        },
      });
      if (callback) callback();
    },

    //指派人员提交
    *selectAssignor({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(selectAllot, payload);
      if (response.result && response.result.code == 200) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        yield put({
          type: 'show',
          payload: {
            systemAssignorList: response.data,
            // systemResult: response.result
          },
        });
      } else if (response && response.result) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        message.error(response.result.message);
      }
      if (callback) callback();
    },

    //分派提交
    *systemCommitAssign({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(systemCommitAssign, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          systemResult: response.result,
        },
      });
      if (callback) callback();
    },

    //获取已经生成的规则树
    *getRoluesTree({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(getRoluesTree, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      const treeData = response.data;
      const newData = treeData.map(record => {
        record.level = 0;
        record.children = record.children.map(item => {
          item.level = 1;
          item.children = item.children.map(option => {
            option.level = 2;
            return option;
          });
          return item;
        });
        return record;
      });

      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          systemRoluesTreeList: newData,
        },
      });
      if (callback) callback();
    },

    //获取按照本次选择返回的规则树
    *getNodeAssignRole({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          systemLoading: true,
        },
      });
      const response = yield call(getNodeAssignRole, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            systemLoading: false,
          },
        });
        return false;
      }
      const treeData = response.data;
      const newData = treeData.map(record => {
        record.level = 0;
        record.children = record.children.map(item => {
          item.level = 1;
          item.children = item.children.map(option => {
            option.level = 2;
            return option;
          });
          return item;
        });
        return record;
      });

      yield put({
        type: 'show',
        payload: {
          systemLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          systemNodeAssignRoleList: newData,
        },
      });
      if (callback) callback();
    },

    //制定机器规则-保存规则树
    *saveTree({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(saveTree, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          systemResult: response.result,
        },
      });
      if (callback) callback();
    },

    //获取规则树
    *nodeAssignUpdate({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          userLoading: true,
        },
      });
      const response = yield call(nodeAssignUpdate, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            userLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          userLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          systemResult: response.result,
        },
      });
      if (callback) callback();
    },

    //获取产品列表及分期列表--新增
    *getProductDiviList({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          systemLoading: true,
        },
      });
      const response = yield call(getProductDiviList, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            systemLoading: false,
          },
        });
        return false;
      }
      yield put({
        type: 'show',
        payload: {
          systemLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          systemProductDiviList: response.data,
        },
      });
      if (callback) callback();
    },

    //新增
    *changeAddProduct({ payload, callback }, { select, call, put }) {
      yield put({
        type: 'addProductCheckStatus',
        payload,
      });
      if (callback) callback();
    }, //编辑
    *changeProductRelateBranch({ payload, callback }, { select, call, put }) {
      yield put({
        type: 'saveNewCheckStatus',
        payload,
      });
      if (callback) callback();
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setValue(state, action) {
      return {
        ...state,
        detailValue: action.payload,
      };
    },
    getStructureId(state, action) {
      return {
        ...state,
        structureId: action.payload.id,
        stateStructure: action.payload.stateStructure,
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    setSystemBranchIdValue(state, action) {
      return {
        ...state,
        systemBranchId: action.payload,
      };
    },

    setSystemlValue(state, action) {
      return {
        ...state,
        systemDetailValue: action.payload,
      };
    },
    //新增组织架构的代理权限
    addProductCheckStatus(state, { payload }) {
      const { item } = payload;
      const systemProductDiviList = state.systemProductDiviList;
      const newStructureProductDiviList = systemProductDiviList.map(
        oldItem => (oldItem.id == item.id ? item : oldItem)
      );
      return {
        ...state,
        systemProductDiviList: newStructureProductDiviList,
      };
    },
    /**
     * 注意： 新增和编辑代理权限的两个方法不可合并，新增会掉代理权限接口，编辑代理权限从详情获取
     */
    //编辑组织架构的代理权限======
    saveNewCheckStatus(state, { payload }) {
      const { item } = payload;
      const systemEditProductDiviList = state.systemEditProductDiviList;
      const newStructureProductDiviList = systemEditProductDiviList.map(
        oldItem => (oldItem.id == item.id ? item : oldItem)
      );
      return {
        ...state,
        systemEditProductDiviList: newStructureProductDiviList,
      };
    },
  },
};
